package com.oa.android.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.oa.android.R;
import com.oa.android.utils.UConstants;
import com.oa.android.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * 新建文件夹
 *
 * @author -zouqh
 */
public class NewFileActivity extends Activity implements OnClickListener {

    private ImageView close;
    private TextView title, send;
    private EditText  emilTitle, emilContent,bianhao,fileName;
    ProgressDialog dialog;
    private RadioGroup group;
    private RadioButton radioButton1,radioButton2;
    private String type="";
    private String dfis;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nwe_file_activity);
        dfis=getIntent().getStringExtra("df");
        type=getIntent().getStringExtra("type");
        close = (ImageView) findViewById(R.id.close);
        title = (TextView) findViewById(R.id.title_text);
        send = (TextView) findViewById(R.id.ritgh_text);
//        sendTo = (TextView) findViewById(R.id.sendTo);
        emilTitle = (EditText) findViewById(R.id.emilTitle);
        bianhao=(EditText)findViewById(R.id.bianhao);
        fileName = (EditText) findViewById(R.id.fileName);
        emilContent = (EditText) findViewById(R.id.emilContent);
        group=(RadioGroup)findViewById(R.id.group);
        radioButton1=(RadioButton)findViewById(R.id.radio1);
        radioButton2=(RadioButton)findViewById(R.id.radio2);
            send.setText("保存");
            title.setText("新建目录");
        if (type.equals("0")){
            emilTitle.setText("单位文件");
        }


        send.setVisibility(View.VISIBLE);

        close.setOnClickListener(this);
        send.setOnClickListener(this);
//        sendTo.setOnClickListener(this);
//        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                switch (checkedId){
//                    case R.id.radio1:
//                        type="是";
//                        break;
//                    case R.id.radio2:
//                        type="否";
//                        break;
//                }
//            }
//        });
//        group.check(R.id.radio2);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent intent;
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;

            case R.id.ritgh_text:
                String str1 = fileName.getText().toString().trim();
                String str2 = emilTitle.getText().toString();
//                String str3 = emilContent.getText().toString();
                if (!Util.NoNull(str1)) {
                    Toast.makeText(getApplicationContext(), "文件名不能为空！", Toast.LENGTH_SHORT).show();
                } else  if (!Util.NoNull(str2)) {
                    Toast.makeText(getApplicationContext(), "文件类型不能为空！", Toast.LENGTH_SHORT).show();
                } else {

                        newFile(str2,type,str1);


                }
                break;

            case R.id.addFile:
                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 1);
                break;
            case R.id.sendTo:
                startActivityForResult(new Intent(this,PersonActivity.class),2);
                break;

            default:
                break;
        }
    }

    private Uri mPhotoUri = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续


//
        }
    }

    private String getPhotoPath(Context context, Uri photoUri, Intent data) {
        String photoPath = "";
        ContentResolver cr = context.getContentResolver();
        if (photoUri != null) {
            Cursor cursor = cr.query(photoUri, null, null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                int pathIndex = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                photoPath = cursor.getString(pathIndex);
                cursor.close();
            }
        } else {
            if (data != null) {
                Uri uri = data.getData();
                if (uri != null && !TextUtils.isEmpty(uri.getPath())) {
                    photoPath = uri.getPath();
                    if (!(new File(photoPath).exists())) {
                        String[] proj = {MediaStore.Images.Media.DATA};
                        Cursor actualimagecursor = cr.query(uri, proj, null,
                                null, null);
                        int actualImg = actualimagecursor
                                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        actualimagecursor.moveToFirst();
                        if (actualimagecursor != null)
                            photoPath = actualimagecursor.getString(actualImg);
                    }
                }
            }
        }
        return photoPath;
    }




    //上传文件
    public void newFile(String title,  String type, String na) {
        Util.printLog("------------type-----", type);
        dialog = ProgressDialog.show(this, null, "新建中...", true, false);
        RequestParams params = new RequestParams(UConstants.NEW_FILE);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("LX", type);
        params.addBodyParameter("FILENAME", na);
//        params.addBodyParameter("BianHao", bianhao.getText().toString()+"");
//        params.addBodyParameter("DirID", dfis);
//        params.addBodyParameter("IfShare", type);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            Toast.makeText(x.app(), "新建成功！", Toast.LENGTH_LONG).show();
                            Intent intent=new Intent();
                            intent.putExtra("success","1");
                            setResult(RESULT_OK, intent);
                           finish();
                        } else {
                            Toast.makeText(x.app(), "新建失败！", Toast.LENGTH_LONG).show();
                        }
//
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "新建出错！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }


}
